<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:gml="http://www.opengis.net/gml" 
	xmlns:ogr="http://ogr.maptools.org/"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
	xmlns:nuts="http://nuts.geovocab.org/id/"
	xmlns:ngeo="http://geovocab.org/geometry#"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:owl="http://www.w3.org/2002/07/owl#">
	
<xsl:output method="xml" indent="yes" />
<xsl:template match="/ogr:FeatureCollection">

	<xsl:for-each select='gml:featureMember/ogr:NUTS_RG_60M_2006' >

		<xsl:result-document href="{concat('id/',ogr:NUTS_ID,'_geometry.rdf')}">
			<rdf:RDF>
			<rdf:Description rdf:about="{concat('http://nuts.geovocab.org/id/',ogr:NUTS_ID,'_geometry')}">
				<dc:rights>© EuroGeographics for the administrative boundaries.</dc:rights>
				<xsl:apply-templates select="ogr:geometryProperty/child::node()"/>
			</rdf:Description>
			</rdf:RDF>
		</xsl:result-document>

	</xsl:for-each>

</xsl:template>

<xsl:template match="gml:MultiPolygon|gml:MultiSurface">
	<rdf:type rdf:resource="http://geovocab.org/geometry#MultiPolygon"/>
	<xsl:apply-templates select="child::gml:polygonMember|child::gml:surfaceMember"/>
</xsl:template>

<xsl:template match="gml:polygonMember|gml:surfaceMember">
	<ngeo:polygonMember>
		<rdf:Description>
			<xsl:apply-templates select="child::gml:Polygon|child::gml:Surface"/>
		</rdf:Description>
	</ngeo:polygonMember>
</xsl:template>

<xsl:template name="polygon" match="gml:Polygon|gml:Surface">
	<rdf:type rdf:resource="http://geovocab.org/geometry#Polygon"/>
	<xsl:apply-templates select="gml:interior|gml:innerBoundaryIs|gml:exterior|gml:outerBoundaryIs"/>
</xsl:template>

<xsl:template match="gml:interior|gml:innerBoundaryIs">
	<ngeo:interior>
		<rdf:Description>
			<xsl:apply-templates select="child::gml:LinearRing"/>
		</rdf:Description>
	</ngeo:interior>	
</xsl:template>

<xsl:template match="gml:exterior|gml:outerBoundaryIs">
	<ngeo:exterior>
		<rdf:Description>
			<xsl:apply-templates select="child::gml:LinearRing"/>
		</rdf:Description>
	</ngeo:exterior>
</xsl:template>

<xsl:template match="gml:LinearRing">
	<rdf:type rdf:resource="http://geovocab.org/geometry#LinearRing"/>
	<xsl:apply-templates select="child::gml:posList|child::gml:coordinates"/>
</xsl:template>

<xsl:template match="gml:LineString|gml:Curve">
	<rdf:type rdf:resource="http://geovocab.org/geometry#LineString"/>
	<xsl:apply-templates select="child::gml:posList|child::gml:coordinates"/>
</xsl:template>

<xsl:template match="gml:posList|gml:coordinates">
	<ngeo:posList rdf:parseType="Collection">
		<xsl:call-template name="format-posList">
			<xsl:with-param name="text" select="self::node()"/>
		</xsl:call-template>
	</ngeo:posList>

</xsl:template>

<xsl:template name="format-posList">
  <xsl:param name="text"/>
  <xsl:choose>
    <xsl:when test="not(contains($text, ','))">
		<xsl:value-of select="$text"/>
    </xsl:when>
    <xsl:otherwise>
		<rdf:Description>
			<xsl:variable name="coordinate" select="replace($text, '\s+.*', '')" />
			<geo:long><xsl:value-of select="substring-before($coordinate, ',')"/></geo:long>
			<geo:lat><xsl:value-of select="substring-after($coordinate, ',')"/></geo:lat>
		</rdf:Description>
		<xsl:call-template name="format-posList">
			<xsl:with-param name="text" select="substring-after($text, ' ')"/>
		</xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>